<div class="smart-rightPanel layui-form">
    <div class="layui-anim layui-anim-upbit">
        <blockquote class="site-text layui-elem-quote">
            <div class="layui-form-item wx-search" id="list_form">
                <div class="layui-inline">
                    <input type="text" name="name" placeholder="请输入渠道名称" autocomplete="off" class="layui-input">
                </div>
                <button class="layui-btn btn-primary" id="search">搜索</button>
                <div class="layui-inline right">
                    <button class="layui-btn btn-primary" id="save">新建渠道</button>
                    <button class="layui-btn btn-danger" id="del">批量删除</button>
                </div>
            </div>
        </blockquote>
        <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
    </div>
</div>
<script>
    layui.use(['layer', 'table', 'form','jquery'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var  $ = layui.jquery;
        var tableObj = table.render({
            id: 'list_table'
            , elem: '#list_table'
            , url: 'buss/channel'
            , align: "center"
            , cols: [[ //表头
                {type: 'checkbox'}
                , {field: 'id', title: '渠道编号',align: 'center'}
                , {field: 'name', title: '渠道名称',  align: 'center'}
                , {field: 'url', title: '渠道链接',  align: 'center'}
                , {field: 'isline', title: '是否上线',  align: 'center', templet: function (d) {
                            return  d.isline==1?'是':'否';
                    }}

                , {field: 'lock', title: '操作', width: 140, templet: function (d) {
                        return '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="edit">修改</button>';
                    }, unresize: true, align: 'center'}
            ]]
            , done :function(res,curr,count){
                $(".layui-table-box").find("[data-field='id']").css("display","none");
            }
        });

        $("#search").click(function () {
            reloadTable(tableObj);
        });


        //表格内部操作按钮监听
        table.on('tool(mainList)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                showDialog({
                    title: '修改',
                    template: 'edit',
                    saveUrl: 'buss/channel/save',
                    tableObj: tableObj,
                    htmlData: data,
                    width : 750
                })
            }
        });


        // 删除
        $("#del").click(function () {
            var data = table.checkStatus('list_table').data;//已选中数据
            if (data.length == 0) {
                layer.msg("至少选择一条");
                return;
            }
            var arr = [];     //选中数组
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].id)
            }
            showConfirm("确认删除？", function () {
                $.ajax({
                    url: 'buss/channel/del',
                    data: {"id": arr.join(",")},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("删除成功");
                            reloadTable(tableObj);
                        } else {
                            layer.msg("删除失败");
                        }
                    }
                })
            });
        });

        //表格内部操作按钮监听
        table.on('tool(mainList)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                showDialog({
                    title: '修改'
                    , template: 'edit'
                    , saveUrl: 'buss/channel/save'
                    , tableObj: tableObj
                    , width: 750
                    , htmlData: data
                    ,beforeSubmit:function (data) {
                        if (data.isline=='-1') {
                            layer.msg("请选择类型");
                            return false;
                        }
                        return true;
                    }
                })

            }
        });


        $("#save").click(function () {
            showDialog({
                title: '添加'
                , template: 'add'
                , saveUrl: 'buss/channel/save'
                , tableObj: tableObj
                , width: 750
                ,beforeSubmit:function (data) {
                    if (data.isline=='-1') {
                        layer.msg("请选择类型");
                        return false;
                    }
                    return true;
                }
            })
        });

    });
</script>

